package com.ybkj.o2o.mc.controller.res;


import com.google.common.base.Preconditions;
import com.google.gson.Gson;
import com.ybkj.o2o.mc.common.DataGrid;
import com.ybkj.o2o.mc.common.PhoneNumberHelper;
import com.ybkj.o2o.mc.common.query.PassengerQuery;
import com.ybkj.o2o.mc.common.query.PassengerQuery2;
import com.ybkj.o2o.mc.common.transport.Error;
import com.ybkj.o2o.mc.common.transport.Result;
import com.ybkj.o2o.mc.entity.Department;
import com.ybkj.o2o.mc.entity.Enterprise;
import com.ybkj.o2o.mc.entity.Passenger;
import com.ybkj.o2o.mc.entity.PassengerAccount;
import com.ybkj.o2o.mc.entity.PassengerGrade;
import com.ybkj.o2o.mc.manager.DepartmentManager;
import com.ybkj.o2o.mc.manager.EnterpriseManager;
import com.ybkj.o2o.mc.manager.PassengerGradeManager;
import com.ybkj.o2o.mc.manager.PassengerManager;

import org.apache.commons.lang.BooleanUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by shine on 15/8/25.
 */
@RestController
@RequestMapping(value = "api/passengers")
public class PassengerRes {

  @Autowired
  private PassengerManager passengerManager;

  @Autowired
  private EnterpriseManager enterpriseManager;

  @Autowired
  private PassengerGradeManager gradeManager;
  
  @Autowired
  private DepartmentManager departmentManager;
  
  /**
   * 创建乘客.
   */
  @RequestMapping(value = {"", "/"}, method = RequestMethod.POST)
  public ResponseEntity<Object> create(
          @RequestParam(required = false, value = "name") String name,
          @RequestParam("phone") String phone,
          @RequestParam(required = false, value = "password") String password,
          @RequestParam(required = false, value = "jobNumber") String jobNumber,
          @RequestParam(required = false, value = "allowOverdraw") Boolean allowOverdraw,
          @RequestParam(required = false, value = "plateNumber") String plateNumber,
          @RequestParam(required = false, value = "photo") String photo,
          @RequestParam(required = false, value = "grade_id") Long gradeId,
          @RequestParam(required = false, value = "idcard") String idcard,
          @RequestParam(required = false, value = "gender") Integer gender,
          @RequestParam(required = false, value = "email") String email,
          @RequestParam(required = false, value = "memo") String memo,
          @RequestParam(required = false, value = "channelId") Long channelId,
          @RequestParam(required = false, value = "channel_name") String channelName,
          @RequestParam(required = false, value = "appVersion") String appVersion,
          @RequestParam(required = false, value = "companyId") Long companyId,
          @RequestParam(required = false, value = "companyName") String companyName,
          @RequestParam(required = false, value = "enterpriseId") Long enterpriseId,
          @RequestParam(required = false, value = "enterpriseName") String enterpriseName,
          @RequestParam(required = false, value = "departmentId") Long departmentId,
          @RequestParam(required = false, value = "departmentName") String departmentName,
          @RequestParam(required = false, value = "introducer") String introducer,
          @RequestParam(required = false, value = "introduceType") Integer introduceType,
          @RequestParam(required = false, value = "wxHeadImgUrl") String wxHeadImgUrl,
          @RequestParam(required = false, value = "wxNickName") String wxNickName,
          @RequestParam("appKey") String appKey) {
    ResponseEntity<Object> responseEntity = null;

    phone = StringUtils.trimToEmpty(phone);
    companyName = StringUtils.trimToEmpty(companyName);
    appKey = StringUtils.trimToEmpty(appKey);
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
    Preconditions.checkArgument(StringUtils.isNotBlank(phone), "乘客电话不能为空");


    if (!PhoneNumberHelper.isPhoneOrTel(phone)) {
      Result<Passenger> result = new Result<>(-1, "乘客电话号码不合法", null);
      responseEntity = new ResponseEntity<>(result, HttpStatus.UNPROCESSABLE_ENTITY);
      return responseEntity;
    }

    if (null == companyId || StringUtils.isBlank(companyName)) {
      Result<Passenger> result = new Result<>(-2, "乘客所属公司信息错误", null);
      responseEntity = new ResponseEntity<>(result, HttpStatus.UNPROCESSABLE_ENTITY);
      return responseEntity;
    }

    //验证乘客是否已经存在
    boolean exists = passengerManager.existsByPhone(phone, appKey);
    if (exists ) {
    	Result<Passenger> result = new Result<>(-3, "手机号已经存在", null);
    	responseEntity = new ResponseEntity<>(result, HttpStatus.UNPROCESSABLE_ENTITY);
    	return responseEntity;
    }
    
    //通过工号验证乘客
    Passenger passengerByJobNumber = passengerManager.findByJobNumberAndAppKey(jobNumber, appKey);
    if (null != passengerByJobNumber) {
      Result<Passenger> result = new Result<>(-3, "工号已经存在", null);
      responseEntity = new ResponseEntity<>(result, HttpStatus.UNPROCESSABLE_ENTITY);
      return responseEntity;
    }


    //保存乘客信息
    try {
		Passenger passenger = new Passenger();
		passenger.setPhone(phone);
		passenger.setAllowOverdraw(BooleanUtils.toBoolean(allowOverdraw));
		passenger.setInBlackList(false);
		passenger.setAppKey(appKey);
		passenger.setDrawMoney(0.0);
		passenger.setNodrawMoney(0.0);
		passenger.setFrozenMoney(0.0);
		passenger.setReceiptMoney(0.0);
		passenger.setChannelId(channelId);
		passenger.setMemo(memo);
		passenger.setChannelName(channelName);
		passenger.setIntroducer(introducer);
		passenger.setIntroduceType(introduceType);
		passenger.setPlateNumber(plateNumber);
		passenger.setAppVersion(appVersion);
		passenger.setPhoto(photo);
		passenger.setType(0);
		passenger.setPassword(password);
		passenger.setJobNumber(jobNumber);
        passenger.setReward(1);
        passenger.setWxHeadImgUrl(wxHeadImgUrl);
        passenger.setWxNickName(wxNickName);
		if (StringUtils.isNotBlank(name)) {
			if (name.length() > 10) {
				Result<Passenger> result = new Result<>(-6, "乘客名称过长", null);
				responseEntity = new ResponseEntity<>(result,
						HttpStatus.UNPROCESSABLE_ENTITY);
				return responseEntity;
			} else {
				passenger.setName(name);
			}
		} else {
			passenger.setName("先生/女士");
		}
		
		PassengerGrade grade = null;
		if (null != gradeId && gradeId != 0) {
			grade = gradeManager.fetch(gradeId, null, appKey);
		} else {
			grade = gradeManager.findDefault(companyId, appKey);
		}
		if (null != grade) {
			passenger.setGradeId(grade.getId());
			passenger.setGradeName(grade.getName());
		}
		
		if (null != companyId) {
			passenger.setCompanyId(companyId);
			passenger.setCompanyName(companyName);
		}

		if (null != enterpriseId) {
			passenger.setType(1);
			Enterprise enterprise = enterpriseManager.findOne(enterpriseId, appKey);
			passenger.setEnterpriseId(enterprise.getId());
			passenger.setEnterpriseName(enterprise.getName());
			passenger.setAllowOverdraw(enterprise.getAllowOverdraw());
			if(null != enterprise){
				PassengerGrade passengerGrade = gradeManager.fetch(enterprise.getGradeId(), null, appKey);
				if(null != passengerGrade){
					passenger.setGradeId(passengerGrade.getId());
					passenger.setGradeName(passengerGrade.getName());
				}
			}
		} else {
			passenger.setEnterpriseId(enterpriseId);
			passenger.setEnterpriseName(enterpriseName);
		}
		
		if (null != departmentId) {
			passenger.setType(1);
			Department department = departmentManager.findByIdAndAppKey(departmentId, appKey);
			passenger.setCompanyId(department.getCompanyId());
			passenger.setCompanyName(department.getCompanyName());
			passenger.setEnterpriseId(department.getEnterpriseId());
			passenger.setEnterpriseName(department.getEnterpriseName());
			passenger.setDepartmentId(department.getId());
			passenger.setDepartmentName(department.getName());
			
			Enterprise enterprise = enterpriseManager.findOne(department.getEnterpriseId(), appKey);
			if(null != enterprise){
				PassengerGrade passengerGrade = gradeManager.fetch(enterprise.getGradeId(), null, appKey);
				if(null != passengerGrade){
					passenger.setGradeId(passengerGrade.getId());
					passenger.setGradeName(passengerGrade.getName());
				}
			}
			
		} else {
			passenger.setDepartmentId(departmentId);
			passenger.setDepartmentName(departmentName);
		}
		if(null == departmentId && null == enterpriseId){
			passenger.setType(0);
			passenger.setJobNumber(null);
			passenger.setDepartmentId(null);
			passenger.setDepartmentName(null);
			passenger.setEnterpriseId(enterpriseId);
			passenger.setEnterpriseName(null);
		}
		if (StringUtils.isNotBlank(idcard)) {
			if (passengerManager.existsByIdcard(idcard, appKey)) {
				Result<Passenger> result = new Result<>(-4, "身份证号已经存在",
						null);
				responseEntity = new ResponseEntity<>(result,
						HttpStatus.UNPROCESSABLE_ENTITY);
				return responseEntity;
			} else {
				passenger.setIdcard(idcard);
			}
		} else {
			passenger.setIdcard(null);
		}

		if (gender != null) {
			passenger.setGender(gender);
		} else {
			passenger.setGender(0);
		}
		if (StringUtils.isNotBlank(email)) {
			if (passengerManager.existsByEmail(email, appKey)) {
				Result<Passenger> result = new Result<>(-5, "邮箱已经存在", null);
				responseEntity = new ResponseEntity<>(result,
						HttpStatus.UNPROCESSABLE_ENTITY);
				return responseEntity;
			} else {
				passenger.setEmail(email);
			}
		} else {
			passenger.setEmail(null);
		}

	  Gson gson = new Gson();
		
	  passenger = passengerManager.insert(passenger);
	  

      Result<Passenger> result = new Result<>(0, "成功", passenger);

      responseEntity = new ResponseEntity<>(result, HttpStatus.CREATED);
      return responseEntity;

    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());

      responseEntity = new ResponseEntity<>(error, HttpStatus.INTERNAL_SERVER_ERROR);

      return responseEntity;
    }
  }

  /**
   * 修改乘客完成订单信息.
   */
  @RequestMapping(value = "updateByDate/{passengerId}", method = RequestMethod.PUT)
  public ResponseEntity<Object> updateByDate(
          @PathVariable("passengerId") Long passengerId, 
          @RequestParam(required = false,value="daijiaFinishTime" ) String daijiaFinishTime,
          @RequestParam(required = false,value="errandFinishTime") String errandFinishTime,
          @RequestParam(required = false,value="freightFinishTime") String freightFinishTime,
          @RequestParam(required = false,value="zhuancheFinishTime") String zhuancheFinishTime,
          @RequestParam(required = false,value="zhuanxianFinishTime") String zhuanxianFinishTime,
          @RequestParam("appKey") String appKey) {
	  
    ResponseEntity<Object> responseEntity = null; 
    appKey = StringUtils.trimToEmpty(appKey);
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空"); 
    
    Passenger passenger = passengerManager.findByIdAndAppKey(passengerId, appKey);
    Preconditions.checkArgument(passenger != null, "此乘客不存在");
    Passenger local = new Passenger();  
    
    //修改乘客信息
    try {    
			if (null != daijiaFinishTime) {
				passenger.setDaijiaFinishTime(new Date(Long.parseLong(daijiaFinishTime)));
			}
			if (null != errandFinishTime ) {
				passenger.setErrandFinishTime(new Date(Long.parseLong(errandFinishTime)));
			}
			if (null != freightFinishTime) {
				passenger.setFreightFinishTime(new Date(Long.parseLong(freightFinishTime)));
			}
			if (null != zhuancheFinishTime) {
				passenger.setZhuancheFinishTime(new Date(Long.parseLong(zhuancheFinishTime)));
			}
			if (null != zhuanxianFinishTime) {
				passenger.setZhuanxianFinishTime(new Date(Long.parseLong(zhuanxianFinishTime)));
			} 
	    	
	      passengerManager.updateByDate(passenger);
	      Result<Passenger> result = new Result<>(0, "成功", null);
	      
	      responseEntity = new ResponseEntity<>(result, HttpStatus.CREATED);
	      return responseEntity;
	    } catch (Exception ex) {
	      ex.printStackTrace();
	      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());
	      responseEntity = new ResponseEntity<>(error, HttpStatus.INTERNAL_SERVER_ERROR);
	      return responseEntity;
	    }
  }
  
  /**
   * 修改乘客基本信息.
   */
  @RequestMapping(value = "{passengerId}", method = RequestMethod.PUT)
  public ResponseEntity<Object> update(
          @PathVariable("passengerId") Long passengerId,
          @RequestParam(required = false, value = "name") String name,
          @RequestParam("phone") String phone,
          @RequestParam(required = false, value = "allowOverdraw") Boolean allowOverdraw,
          @RequestParam(required = false, value = "password") String password,
          @RequestParam(required = false, value = "jobNumber") String jobNumber,
          @RequestParam(required = false, value = "enterpriseId") Long enterpriseId,
          @RequestParam(required = false, value = "enterpriseName") String enterpriseName,
          @RequestParam(required = false, value = "departmentId") Long departmentId,
          @RequestParam(required = false, value = "departmentName") String departmentName,
          @RequestParam(required = false, value = "plateNumber") String plateNumber,
          @RequestParam(required = false, value = "photo") String photo,
          @RequestParam(required = false, value = "grade_id") Long gradeId,
          @RequestParam(required = false, value = "idcard") String idcard,
          @RequestParam(required = false, value = "gender") Integer gender,
          @RequestParam(required = false, value = "email") String email,
          @RequestParam(required = false, value = "memo") String memo,
          @RequestParam(required = false, value = "address") String address,
          @RequestParam(required = false, value = "channelId") Long channelId,
          @RequestParam(required = false, value = "channel_name") String channelName,
          @RequestParam(required = false, value = "companyId") Long companyId,
          @RequestParam(required = false, value = "companyName") String companyName,
          @RequestParam(required = false, value = "introducer") String introducer,
          @RequestParam(required = false, value = "introduceType") Integer introduceType,
          @RequestParam(required = false, value = "appVersion") String appVersion,
          @RequestParam(required = false, value = "drivingNumberTimes") Long drivingNumberTimes,
          @RequestParam(required = false, value = "mileage") Double mileage,
          @RequestParam(required = false, value = "upStatus") Integer upStatus,
          @RequestParam(required = false, value = "photoAuditStatus") Integer photoAuditStatus,
          @RequestParam(required = false, value = "idCardPhoto") String idCardPhoto,
          @RequestParam(required = false, value = "idCardBackPhoto") String idCardBackPhoto,
          @RequestParam("appKey") String appKey) {
	  
    ResponseEntity<Object> responseEntity = null;
    phone = StringUtils.trimToEmpty(phone);
    appKey = StringUtils.trimToEmpty(appKey);
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
    Preconditions.checkArgument(StringUtils.isNotBlank(phone), "乘客电话不能为空");
    if (!PhoneNumberHelper.isPhoneOrTel(phone)) {
      Result<Passenger> result = new Result<>(-1, "乘客电话号码不合法", null);
      responseEntity = new ResponseEntity<>(result, HttpStatus.UNPROCESSABLE_ENTITY);
      return responseEntity;
    }
    Passenger passenger = passengerManager.findByIdAndAppKey(passengerId, appKey);
    Preconditions.checkArgument(passenger != null, "此乘客不存在");
    Passenger local = new Passenger();
    
    if(StringUtils.isNotBlank(phone)){
    	local = passengerManager.findByPhoneAndAppKey(phone, appKey);
    	if (null != local && !local.getId().equals(passengerId)) {
    	  Result<Passenger> result = new Result<>(-3, "电话号码已存在", null);
  	      responseEntity = new ResponseEntity<>(result, HttpStatus.UNPROCESSABLE_ENTITY);
  	      return responseEntity;
    	}
    }
    if (StringUtils.isNotBlank(email)) {
      local = passengerManager.findByEmailAndAppKey(email, appKey);
      if (null != local && !local.getId().equals(passengerId)) {
        Result<Passenger> result = new Result<>(-3, "邮箱已存在", null);
	    responseEntity = new ResponseEntity<>(result, HttpStatus.UNPROCESSABLE_ENTITY);
	    return responseEntity;
      }
    }
    if (StringUtils.isNotBlank(idcard)) {
      local = passengerManager.findByIdcardAndAppKey(idcard, appKey);
      if (null != local && !local.getId().equals(passengerId)) {
        Result<Passenger> result = new Result<>(-3, "身份证已存在", null);
	    responseEntity = new ResponseEntity<>(result, HttpStatus.UNPROCESSABLE_ENTITY);
	    return responseEntity;
      }
    }
//    if(null != departmentId){
//      local = passengerManager.findByNameAndAppKey(name, appKey);
//      if (null != local && local.getId() != passengerId) {
//          Preconditions.checkArgument(passenger.getId().equals(local.getId()), "存在相同姓名员工");
//      }
//    }
    
    //通过工号验证乘客
    if(null != jobNumber){
    	local = passengerManager.findByJobNumberAndAppKey(jobNumber, appKey);
	    if (null != local && !local.getId().equals(passengerId)) {
	      Result<Passenger> result = new Result<>(-3, "工号已经存在", null);
	      responseEntity = new ResponseEntity<>(result, HttpStatus.UNPROCESSABLE_ENTITY);
	      return responseEntity;
	    }
    }

    //修改乘客信息
    try {
      if (StringUtils.isNotBlank(name)) {
    	  passenger.setName(name);
      }
      if (StringUtils.isNotBlank(phone)) {
    	  passenger.setPhone(phone);
      }
	  if (gender != null) {
		  passenger.setGender(gender);
	  }
	  if (companyId != null) {
		  passenger.setCompanyId(companyId);
	  }
	  if (StringUtils.isNotBlank(companyName)) {
		  passenger.setCompanyName(companyName);
	  }
	  PassengerGrade grade = null;
	  if (gradeId != null) {
		  grade = gradeManager.fetch(gradeId, null, appKey);
		  if (grade != null) {
	   	  passenger.setGradeId(grade.getId());
		  passenger.setGradeName(grade.getName());
	      }
	  }

	  if (allowOverdraw != null) {
          passenger.setAllowOverdraw(allowOverdraw);
      }
      if (StringUtils.isNotBlank(password)) {
          passenger.setPassword(password);
      }
      if (StringUtils.isNotBlank(jobNumber)) {
          passenger.setJobNumber(jobNumber);
      }
      if (null != enterpriseId) {
        if(Long.valueOf(-1).equals(enterpriseId)){
          passenger.setType(0);
          passenger.setEnterpriseId(null);
          passenger.setEnterpriseName(null);
          passenger.setDepartmentId(null);
          passenger.setDepartmentName(null);
          passenger.setJobNumber(null);
          passenger.setPassword(null);
        } else {
    	  passenger.setType(1);
    	  Enterprise enterprise = enterpriseManager.findByIdAndAppKey(enterpriseId, appKey);
    	  if (null != enterprise) {
        	  passenger.setEnterpriseId(enterprise.getId());
        	  passenger.setEnterpriseName(enterprise.getName());
        	  passenger.setAllowOverdraw(enterprise.getAllowOverdraw());
        	  PassengerGrade passengerGrade = gradeManager.fetch(enterprise.getGradeId(), null, appKey);
    			
        	  if(null != passengerGrade){
        	    passenger.setGradeId(passengerGrade.getId());
        	    passenger.setGradeName(passengerGrade.getName());
        	  }
    	  }
        }
      }
      
      if (null != departmentId) {
        if(Long.valueOf(-1).equals(departmentId)) {
          passenger.setType(0);
          passenger.setDepartmentId(null);
          passenger.setDepartmentName(null);
          passenger.setJobNumber(null);
          passenger.setPassword(null);
        } else {
          passenger.setType(1);
    	  Department department = departmentManager.findByIdAndAppKey(departmentId, appKey);
    	  if (null != department) {
        	  passenger.setDepartmentId(departmentId);
        	  passenger.setDepartmentName(department.getName());
        	  Enterprise enterprise = enterpriseManager.findOne(department.getEnterpriseId(), appKey);
    			
        	  if(null != enterprise){
        	    PassengerGrade passengerGrade = gradeManager.fetch(enterprise.getGradeId(), null, appKey);
        	    if(null != passengerGrade){
        	      passenger.setGradeId(passengerGrade.getId());
        	      passenger.setGradeName(passengerGrade.getName());
        	    }
        	  }
    	  }
        }
      }
      
      passenger.setIdcard(idcard);
      passenger.setEmail(email);
      passenger.setChannelId(channelId);
      passenger.setChannelName(channelName);
      passenger.setIntroducer(introducer);
    	  
      if (introduceType != null) {
        passenger.setIntroduceType(introduceType);
      }
    	
      passenger.setPlateNumber(plateNumber);
      passenger.setMemo(memo);
    	  
      if (StringUtils.isNotBlank(address)) {
        passenger.setAddress(address);
      }
      if (StringUtils.isNotBlank(photo)) {
          passenger.setPhoto(photo);
      }
      if (StringUtils.isNotBlank(appVersion)) {
        passenger.setAppVersion(appVersion);
      }
      if (drivingNumberTimes != null) {
        passenger.setDrivingNumberTimes(drivingNumberTimes);
      }
      if (mileage != null) {
        passenger.setMileage(mileage);
      }
      if (upStatus != null) {
    	  passenger.setUpStatus(upStatus);;
      }
      if(photoAuditStatus!=null){
      	passenger.setPhotoAuditStatus(photoAuditStatus);
			}
			if(StringUtils.isNotBlank(idCardPhoto)){
				passenger.setIdCardPhoto(idCardPhoto);
			}
			if(StringUtils.isNotBlank(idCardBackPhoto)){
				passenger.setIdCardBackPhoto(idCardBackPhoto);
			}
      passengerManager.update(passenger);
      Result<Passenger> result = new Result<>(0, "成功", null);
      
      responseEntity = new ResponseEntity<>(result, HttpStatus.CREATED);
      return responseEntity;
    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());
      responseEntity = new ResponseEntity<>(error, HttpStatus.INTERNAL_SERVER_ERROR);
      return responseEntity;
    }
  }

  /**
   * 删除乘客.
   */
  @RequestMapping(value = "{passengerId}", method = RequestMethod.DELETE)
  public ResponseEntity<Object> delete(@PathVariable("passengerId") Long passengerId,
                                       @RequestParam("appKey") String appKey) {
    ResponseEntity<Object> responseEntity = null;

    appKey = StringUtils.trimToEmpty(appKey);
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");

    try {
      passengerManager.deletePassenger(passengerId, appKey);

      Result<Passenger> result = new Result<>(0, "成功", null);

      responseEntity = new ResponseEntity<>(result, HttpStatus.NO_CONTENT);

      return responseEntity;

    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());

      responseEntity = new ResponseEntity<>(error, HttpStatus.INTERNAL_SERVER_ERROR);

      return responseEntity;
    }

  }

  /**
   * 修改乘客的位置.
   */
  @RequestMapping(value = "{passengerId}/address", method = RequestMethod.PUT)
  public ResponseEntity<Object> updateAddress(@PathVariable("passengerId") Long passengerId,
                                              @RequestParam("address") String address,
                                              @RequestParam("latitude") Double latitude,
                                              @RequestParam("longitude") Double longitude,
                                              @RequestParam("appKey") String appKey) {

    ResponseEntity<Object> responseEntity = null;

    appKey = StringUtils.trimToEmpty(appKey);
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");

    Passenger passenger = passengerManager.findByIdAndAppKey(passengerId, appKey);
    Preconditions.checkArgument(passenger != null, "乘客不存在");
    try {
      passenger.setAddress(address);
      passenger.setLatitude(latitude);
      passenger.setLongitude(longitude);

      passengerManager.updateLocation(passenger);
      Result<Passenger> result = new Result<>(0, "成功", null);

      responseEntity = new ResponseEntity<>(result, HttpStatus.CREATED);

      return responseEntity;

    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());

      responseEntity = new ResponseEntity<>(error, HttpStatus.INTERNAL_SERVER_ERROR);

      return responseEntity;
    }
  }

  /**
   * 设置乘客是否为拉黑状态.
   */
  @RequestMapping(value = "{passengerId}/inBlackList", method = RequestMethod.PUT)
  public ResponseEntity<Object> updateInBlackList(
          @PathVariable("passengerId") Long passengerId,
          @RequestParam("inBlackList") Boolean inBlackList,
          @RequestParam("appKey") String appKey) {

    ResponseEntity<Object> responseEntity = null;

    appKey = StringUtils.trimToEmpty(appKey);
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");

    Passenger passenger = passengerManager.findByIdAndAppKey(passengerId, appKey);
    Preconditions.checkArgument(passenger != null, "乘客不存在");

    try {
      passenger.setInBlackList(BooleanUtils.toBoolean(inBlackList));
      passengerManager.updateBlackList(passenger);
      Result<Passenger> result = new Result<>(0, "成功", null);

      responseEntity = new ResponseEntity<>(result, HttpStatus.CREATED);

      return responseEntity;

    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());

      responseEntity = new ResponseEntity<>(error, HttpStatus.INTERNAL_SERVER_ERROR);

      return responseEntity;
    }
  }

  /**
   * 设置乘客的设备类型.
   */
  @RequestMapping(value = "{passengerId}/deviceType", method = RequestMethod.PUT)
  public ResponseEntity<Object> updateDeviceType(
          @PathVariable("passengerId") Long passengerId,
          @RequestParam("deviceType") String deviceType,
          @RequestParam("appKey") String appKey) {

    ResponseEntity<Object> responseEntity = null;

    appKey = StringUtils.trimToEmpty(appKey);
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");

    Passenger passenger = passengerManager.findByIdAndAppKey(passengerId, appKey);
    Preconditions.checkArgument(passenger != null, "乘客不存在");
    try {
      passenger.setDeviceType(deviceType);
      passengerManager.updateDeviceType(passenger);
      Result<Passenger> result = new Result<>(0, "成功", null);

      responseEntity = new ResponseEntity<>(result, HttpStatus.CREATED);

      return responseEntity;

    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());

      responseEntity = new ResponseEntity<>(error, HttpStatus.INTERNAL_SERVER_ERROR);

      return responseEntity;
    }
  }

  /**
   * 修改乘客的开票金额.
   *
   * @param receiptMoney 开票金额(大于0的数字)
   */
  @RequestMapping(value = "{passengerId}/receiptMoney", method = RequestMethod.PUT)
  public ResponseEntity<Object> updateReceiptMoney(
          @PathVariable("passengerId") Long passengerId,
          @RequestParam("receiptMoney") Double receiptMoney,
          @RequestParam("appKey") String appKey) {
    ResponseEntity<Object> responseEntity = null;

    appKey = StringUtils.trimToEmpty(appKey);
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");

    Passenger passenger = passengerManager.findByIdAndAppKey(passengerId, appKey);
    Preconditions.checkArgument(passenger != null, "乘客不存在");

    try {
      Double money = passenger.getReceiptMoney() - receiptMoney;
      if (money < 0) {
        money = 0.0;
      }
      passenger.setReceiptMoney(money);
      passengerManager.updateReceiptMoney(passenger);
      Result<Passenger> result = new Result<>(0, "成功", null);

      responseEntity = new ResponseEntity<>(result, HttpStatus.CREATED);

      return responseEntity;

    } catch (Exception ex) {

      ex.printStackTrace();

      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());

      responseEntity = new ResponseEntity<>(error, HttpStatus.INTERNAL_SERVER_ERROR);

      return responseEntity;
    }
  }

  /**
   * 根据ID查询单个乘客.
   */
  @RequestMapping(value = "{passengerId}", method = RequestMethod.GET)
  public ResponseEntity<Object> getPassenger(@PathVariable("passengerId") Long passengerId,
                                             @RequestParam("appKey") String appKey) {

    ResponseEntity<Object> responseEntity = null;

    appKey = StringUtils.trimToEmpty(appKey);
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");

    Passenger passenger = passengerManager.findByIdAndAppKey(passengerId, appKey);

    try {

      Result<Passenger> result = new Result<>(0, "成功", passenger);

      responseEntity = new ResponseEntity<>(result, HttpStatus.OK);

      return responseEntity;

    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());

      responseEntity = new ResponseEntity<>(error, HttpStatus.INTERNAL_SERVER_ERROR);

      return responseEntity;
    }
  }

  /**
   * 根据电话,邮箱,身份证查询单个乘客.
   */
  @RequestMapping(value = "findone", method = RequestMethod.GET)
  public ResponseEntity<Object> getPassenger(
          @RequestParam(required = false, value = "phone") String phone,
          @RequestParam(required = false, value = "email") String email,
          @RequestParam(required = false, value = "idcard") String idcard,
          @RequestParam(required = false, value = "jobNumber") String jobNumber,
          @RequestParam("appKey") String appKey) {
    ResponseEntity<Object> responseEntity = null;

    appKey = StringUtils.trimToEmpty(appKey);
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");

    try {
      Passenger passenger = null;
      if (StringUtils.isNotBlank(phone)) {
        passenger = passengerManager.findByPhoneAndAppKey(phone, appKey);
      } else if (StringUtils.isNotBlank(email)) {
        passenger = passengerManager.findByEmailAndAppKey(email, appKey);
      } else if (StringUtils.isNotBlank(idcard)) {
        passenger = passengerManager.findByIdcardAndAppKey(idcard, appKey);
      } else if (StringUtils.isNotBlank(jobNumber)){
	    passenger = passengerManager.findByJobNumberAndAppKey(jobNumber, appKey);  
      }
      
      Result<Passenger> result = new Result<>(0, "成功", passenger);

      responseEntity = new ResponseEntity<>(result, HttpStatus.OK);

      return responseEntity;

    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());

      responseEntity = new ResponseEntity<>(error, HttpStatus.INTERNAL_SERVER_ERROR);

      return responseEntity;
    }
  }


  /**
   * 根据条件分页查询.
   */
  @RequestMapping(value = {"", "/"}, method = RequestMethod.GET)
  public ResponseEntity<Object> findByType(
          @RequestParam(required = false, value = "type") Integer type,
          @RequestParam(required = false, value = "gradeId") Long gradeId,
          @RequestParam(required = false, value = "companyId") Long companyId,
          @RequestParam(required = false, value = "start") Long start,
          @RequestParam(required = false, value = "end") Long end,
          @RequestParam(required = false, value = "param") String param,
          @RequestParam(required = false, value = "page") Integer page,
          @RequestParam(required = false, value = "rows") Integer rows,
          @RequestParam("appKey") String appKey) {
    ResponseEntity<Object> responseEntity = null;

    appKey = StringUtils.trimToEmpty(appKey);
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");

    if (type != null) {
      Preconditions.checkArgument(type == 0 || type == 1, "乘客类型错误");
    }

    if (gradeId != null) {
      PassengerGrade grade = gradeManager.fetch(gradeId, null, appKey);
      Preconditions.checkArgument(grade != null, "等级不存在");
    }
    PassengerQuery passengerQuery = new PassengerQuery();
    passengerQuery.setInBlackList(false);
    passengerQuery.setType(type);
    passengerQuery.setGradeId(gradeId);
    passengerQuery.setCompanyId(companyId);
    passengerQuery.setStartTime(start);
    passengerQuery.setEndTime(end);
    passengerQuery.setPage(page);
    passengerQuery.setRows(rows);
    passengerQuery.setKeyword(param);
    passengerQuery.setAppKey(appKey);
    

    try {
      DataGrid<Passenger> dataGrid = passengerManager.findByPassengerQuery(passengerQuery);

      Result<DataGrid<Passenger>> result = new Result<>(0, "成功", dataGrid);

      responseEntity = new ResponseEntity<>(result, HttpStatus.OK);

      return responseEntity;

    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());

      responseEntity = new ResponseEntity<>(error, HttpStatus.INTERNAL_SERVER_ERROR);

      return responseEntity;
    }
  }

  /**
   * 根据公司,乘客类型查询个人和企业的乘客.
   */
  @RequestMapping(value = "type", method = RequestMethod.GET)
  @Deprecated
  public ResponseEntity<Object> findByType(
          @RequestParam(required = false, value = "companyId") Long companyId,
          @RequestParam(required = false, value = "type") Integer type,
          @RequestParam("appKey") String appKey) {

    ResponseEntity<Object> responseEntity = null;

    appKey = StringUtils.trimToEmpty(appKey);
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");

    List<Passenger> passengers = passengerManager.findByTypeOrCompanyId(type, companyId, appKey);

    try {

      Result<List<Passenger>> result = new Result<>(0, "成功", passengers);

      responseEntity = new ResponseEntity<>(result, HttpStatus.OK);

      return responseEntity;

    } catch (Exception ex) {

      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());

      responseEntity = new ResponseEntity<>(error, HttpStatus.INTERNAL_SERVER_ERROR);

      return responseEntity;
    }
  }


  /**
   * 统计.
   */
  @RequestMapping(value = {"findByMultiCondition"}, method = RequestMethod.GET)
  public ResponseEntity<Object> findByMultiCondition(
          @RequestParam(required = false, value = "type") Integer[] type,
          @RequestParam(required = false, value = "gradeId") Long[] grade,
          @RequestParam(required = false, value = "companyId") Long companyId,
          @RequestParam(required = false, value = "companyIds") Long[] companyIds,
          @RequestParam(required = false, value = "permission") Boolean[] permission,
          @RequestParam(required = false, value = "isPlatform") Integer[] isPlatform,
          @RequestParam(required = false, value = "channel_name") String[] channelName,
          @RequestParam(required = false, value = "start") Long start,
          @RequestParam(required = false, value = "end") Long end,
          @RequestParam(required = false, value = "enterpriseId") Long enterpriseId,
          @RequestParam(required = false, value = "departmentId") Long departmentId,
          @RequestParam(required = false, value = "departmentName") String departmentName,
          @RequestParam(required = false, value = "enterpriseName") String enterpriseName,
          @RequestParam(required = false, value = "jobNumber") String jobNumber,
          @RequestParam(required = false, value = "name") String name,
          @RequestParam(required = false, value = "phone") String phone,
          @RequestParam(required = false, value = "introducer") String[] introducer,
          @RequestParam(required = false, value = "carNumber") String carNumber,
          @RequestParam(required = false, value = "page") Integer page,
          @RequestParam(required = false, value = "rows") Integer rows,
          @RequestParam("appKey") String appKey) {

    ResponseEntity<Object> responseEntity = null;
    appKey = StringUtils.trimToEmpty(appKey);
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");

    PassengerQuery2 passengerQuery2 = new PassengerQuery2();
    passengerQuery2.setType(type);
    passengerQuery2.setGradeId(grade);
    passengerQuery2.setCompanyId(companyId);
    passengerQuery2.setCompanyIds(companyIds);
    passengerQuery2.setPermission(permission);
    passengerQuery2.setIsPlatform(isPlatform);
    passengerQuery2.setChannelName(channelName);
    passengerQuery2.setStartTime(start);
    passengerQuery2.setEndTime(end);
    passengerQuery2.setEnterpriseId(enterpriseId);
    passengerQuery2.setDepartmentId(departmentId);
    passengerQuery2.setDepartmentName(departmentName);
    passengerQuery2.setEnterpriseName(enterpriseName);
    passengerQuery2.setJobNumber(jobNumber);
    passengerQuery2.setName(name);
    passengerQuery2.setPhone(phone);
    passengerQuery2.setCarNumber(carNumber);
    passengerQuery2.setPage(page);
    passengerQuery2.setRows(rows);
    passengerQuery2.setAppKey(appKey);
	passengerQuery2.setIntroducer(introducer);

    try {

      DataGrid<Passenger> dataGrid = passengerManager.findByPassengerQuery2(passengerQuery2);
      
      Result<DataGrid<Passenger>> result = new Result<>(0, "成功", dataGrid);
      
      responseEntity = new ResponseEntity<>(result, HttpStatus.OK) ;

      return responseEntity;

    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());

      responseEntity = new ResponseEntity<>(error, HttpStatus.INTERNAL_SERVER_ERROR);

      return responseEntity;
    }
  }

  /**
   * 统计条数.
   *
   * @return 数字
   */
  @RequestMapping(value = {"countByMultiCondition"}, method = RequestMethod.GET)
  public ResponseEntity<Object> countByMultiCondition(
          @RequestParam(required = false, value = "type") Integer[] type,
          @RequestParam(required = false, value = "gradeId") Long[] grade,
          @RequestParam(required = false, value = "companyId") Long companyId,
          @RequestParam(required = false, value = "companyIds") Long[] companyIds,
          @RequestParam(required = false, value = "permission") Boolean[] permission,
          @RequestParam(required = false, value = "channel_name") String[] channelName,
          @RequestParam(required = false, value = "start") Long start,
          @RequestParam(required = false, value = "end") Long end,
          @RequestParam(required = false, value = "name") String name,
          @RequestParam(required = false, value = "phone") String phone,
          @RequestParam(required = false, value = "carNumber") String carNumber,
          @RequestParam("appKey") String appKey) {

    ResponseEntity<Object> responseEntity = null;
    appKey = StringUtils.trimToEmpty(appKey);
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");

    try {

      PassengerQuery2 passengerQuery2 = new PassengerQuery2();
      passengerQuery2.setType(type);
      passengerQuery2.setGradeId(grade);
      passengerQuery2.setCompanyId(companyId);
      passengerQuery2.setCompanyIds(companyIds);
      passengerQuery2.setPermission(permission);
      passengerQuery2.setChannelName(channelName);
      passengerQuery2.setStartTime(start);
      passengerQuery2.setEndTime(end);
      passengerQuery2.setName(name);
      passengerQuery2.setPhone(phone);
      passengerQuery2.setCarNumber(carNumber);
      passengerQuery2.setAppKey(appKey);

      Long count = passengerManager.countByPassengerQuery2(passengerQuery2);

      Result<Long> result = new Result<>(0, "成功", count);

      responseEntity = new ResponseEntity<>(result, HttpStatus.OK);

      return responseEntity;
    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());

      responseEntity = new ResponseEntity<>(error, HttpStatus.INTERNAL_SERVER_ERROR);

      return responseEntity;
    }
  }

  /**
   * 根据电话模糊分页查询乘客.
   */
  @RequestMapping(value = "phoneLike", method = RequestMethod.GET)
  public ResponseEntity<Object> findLikePhone(
          @RequestParam("phone") String phone,
          @RequestParam(required = false, value = "page") Integer page,
          @RequestParam(required = false, value = "rows") Integer rows,
          @RequestParam("appKey") String appKey) {

    ResponseEntity<Object> responseEntity = null;

    appKey = StringUtils.trimToEmpty(appKey);
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
    Preconditions.checkArgument(StringUtils.isNotBlank(phone), "电话匹配不能为空");

    try {

      PassengerQuery passengerQuery = new PassengerQuery();
      passengerQuery.setInBlackList(false);
      passengerQuery.setPhone(phone);
      passengerQuery.setPage(page);
      passengerQuery.setRows(rows);
      passengerQuery.setAppKey(appKey);

      DataGrid<Passenger> dataGrid = passengerManager.findByPassengerQuery(passengerQuery);

      Result<DataGrid<Passenger>> result = new Result<>(0, "成功", dataGrid);

      responseEntity = new ResponseEntity<>(result, HttpStatus.OK);

      return responseEntity;

    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());

      responseEntity = new ResponseEntity<>(error, HttpStatus.INTERNAL_SERVER_ERROR);

      return responseEntity;
    }
  }

  /**
   * 根据推荐人和推荐类型 (分页)查询乘客.
   *
   * @param introducer    推荐人
   * @param introduceType 推荐类型
   * @param page          分页参数
   * @param rows          分页参数
   */
  @RequestMapping(value = "introducer", method = RequestMethod.GET)
  public ResponseEntity<Object> findByIntroducer(
          @RequestParam("introducer") String introducer,
          @RequestParam("introduceType") Integer introduceType,
          @RequestParam(required = false, value = "page") Integer page,
          @RequestParam(required = false, value = "rows") Integer rows,
          @RequestParam("appKey") String appKey) {

    ResponseEntity<Object> responseEntity = null;
    appKey = StringUtils.trimToEmpty(appKey);
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
    Preconditions.checkArgument(StringUtils.isNotBlank(introducer), "推荐人不能为空");
    Preconditions.checkArgument(introduceType != null, "推荐人类型不能为空");

    try {

      PassengerQuery passengerQuery = new PassengerQuery();
      passengerQuery.setInBlackList(false);
      passengerQuery.setIntroducer(introducer);
      passengerQuery.setIntroduceType(introduceType);
      passengerQuery.setPage(page);
      passengerQuery.setRows(rows);
      passengerQuery.setAppKey(appKey);

      DataGrid<Passenger> dataGrid = passengerManager.findByPassengerQuery(passengerQuery);

      Result<DataGrid<Passenger>> result = new Result<>(0, "成功", dataGrid);

      responseEntity = new ResponseEntity<>(result, HttpStatus.OK);

      return responseEntity;

    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());

      responseEntity = new ResponseEntity<>(error, HttpStatus.INTERNAL_SERVER_ERROR);

      return responseEntity;
    }
  }

  /**
   * 分页查询黑名单.
   */
  @RequestMapping(value = "inBlackList", method = RequestMethod.GET)
  public ResponseEntity<Object> getBacklist(
          @RequestParam(required = false, value = "companyId") Long companyId,
          @RequestParam(required = false, value = "param") String param,
          @RequestParam("page") Integer page,
          @RequestParam("rows") Integer rows,
          @RequestParam("appKey") String appKey) {

    ResponseEntity<Object> responseEntity = null;

    appKey = StringUtils.trimToEmpty(appKey);
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");

    try {

      PassengerQuery passengerQuery = new PassengerQuery();
      passengerQuery.setInBlackList(true);
      passengerQuery.setKeyword(param);
      passengerQuery.setCompanyId(companyId);
      passengerQuery.setPage(page);
      passengerQuery.setRows(rows);
      passengerQuery.setAppKey(appKey);

      DataGrid<Passenger> dataGrid = passengerManager.findByPassengerQuery(passengerQuery);
      Result<DataGrid<Passenger>> result = new Result<>(0, "成功", dataGrid);

      responseEntity = new ResponseEntity<>(result, HttpStatus.OK);

      return responseEntity;

    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());

      responseEntity = new ResponseEntity<>(error, HttpStatus.INTERNAL_SERVER_ERROR);

      return responseEntity;
    }
  }


  /**
   * 单个乘客充值.
   */
  @RequestMapping(value = "{passengerId}/passengerAccountDetail", method = RequestMethod.POST)
  public ResponseEntity<Object> createAccount(
          @PathVariable("passengerId") Long passengerId,
          @RequestParam("money") Double money,
          @RequestParam("payType") String payType,
          @RequestParam("operator") String operator,
          @RequestParam("updateBlance") Boolean updateBlance,//修改余额
          @RequestParam(required = false, value = "introducerId") Long introducerId,
          @RequestParam(required = false, value = "useNoDrawMoney") Boolean useNoDrawMoney,
          @RequestParam(required = false, value = "details") String details,
          @RequestParam(required = false, value = "tradeNo") String tradeNo,
          @RequestParam(required = false, value = "orderNo") String orderNo,
          @RequestParam(required = false, value = "orderId") Long orderId,
          @RequestParam(required = false, value = "orderType") Integer orderType,
          @RequestParam("appKey") String appKey) {
    ResponseEntity<Object> responseEntity = null;
    appKey = StringUtils.trimToEmpty(appKey);
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
    Preconditions.checkArgument(StringUtils.isNotBlank(payType), "支付类型不能为空");
    Preconditions.checkArgument(StringUtils.isNotBlank(operator), "操作人不能为空");
    Passenger passenger = passengerManager.findByIdAndAppKey(passengerId, appKey);
    Preconditions.checkArgument(passenger != null, "乘客不存在");
    try {
      updateBlance = BooleanUtils.toBoolean(updateBlance);
      useNoDrawMoney = BooleanUtils.toBoolean(useNoDrawMoney);
      PassengerAccount account = new PassengerAccount();
      account.setPassengerId(passenger.getId());
      account.setPassengerPhone(passenger.getPhone());
      account.setPassengerName(passenger.getName());
      account.setMoney(money);
      account.setOrderNo(orderNo);
      account.setOrderId(orderId);
      account.setOrderType(orderType);
      account.setDetails(details);
      account.setTradeNo(tradeNo);
      account.setPayType(payType);
      account.setOperator(operator);
      account.setAppKey(appKey);
      account.setCompanyId(passenger.getCompanyId());

      account.setIntroducerId(introducerId);

      PassengerAccount passengerAccount = passengerManager.updateAccount(account, passenger,
                      updateBlance,useNoDrawMoney);

      Result<PassengerAccount> result = new Result<>(0, "成功", passengerAccount);
      responseEntity = new ResponseEntity<>(result, HttpStatus.CREATED);

      return responseEntity;

    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());

      responseEntity = new ResponseEntity<>(error, HttpStatus.INTERNAL_SERVER_ERROR);

      return responseEntity;
    }
  }

  /**
   * 更新乘客不可提现金额.
   */
  @RequestMapping(value = "{passengerId}/passengerNoDrawMoney", method = RequestMethod.POST)
  public ResponseEntity<Object> passengerNoDrawMoney(
          @PathVariable("passengerId") Long passengerId,
          @RequestParam("money") Double money,
          @RequestParam("payType") String payType,
          @RequestParam("operator") String operator,
          @RequestParam("updateBlance") Boolean updateBlance,
          @RequestParam(required = false, value = "details") String details,
          @RequestParam(required = false, value = "tradeNo") String tradeNo,
          @RequestParam(required = false, value = "orderNo") String orderNo,
          @RequestParam(required = false, value = "orderId") Long orderId,
          @RequestParam(required = false, value = "orderType") Integer orderType,
          @RequestParam("appKey") String appKey) {

    ResponseEntity<Object> responseEntity = null;
    appKey = StringUtils.trimToEmpty(appKey);
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
    Preconditions.checkArgument(StringUtils.isNotBlank(payType), "支付类型不能为空");
    Preconditions.checkArgument(StringUtils.isNotBlank(operator), "操作人不能为空");
    Passenger passenger = passengerManager.findByIdAndAppKey(passengerId, appKey);
    Preconditions.checkArgument(passenger != null, "乘客不存在");
    try {

      PassengerAccount account = new PassengerAccount();
      account.setMoney(money);
      account.setOrderNo(orderNo);
      account.setOrderId(orderId);
      account.setOrderType(orderType);
      account.setDetails(details);
      account.setTradeNo(tradeNo);
      account.setPayType(payType);
      account.setOperator(operator);
      account.setAppKey(appKey);
      account.setPassengerId(passenger.getId());
      account.setPassengerPhone(passenger.getPhone());
      account.setPassengerName(passenger.getName());
      account.setCompanyId(passenger.getCompanyId());

      PassengerAccount passengerAccount = passengerManager.updateNoDrawMoney(account, passenger,
              updateBlance);

      Result<PassengerAccount> result = new Result<>(0, "成功", passengerAccount);
      responseEntity = new ResponseEntity<>(result, HttpStatus.CREATED);

      return responseEntity;

    } catch (Exception ex) {

      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());

      responseEntity = new ResponseEntity<>(error, HttpStatus.INTERNAL_SERVER_ERROR);

      return responseEntity;
    }
  }

  /**
   * 根据公司Id查询乘客总数.
   */
  @RequestMapping(value = "countByCompany", method = RequestMethod.GET)
  public ResponseEntity<Object> countByCompany(
          @RequestParam(required = false, value = "companyId") Long companyId,
          @RequestParam(required = false, value = "start") Long start,
          @RequestParam(required = false, value = "end") Long end,
          @RequestParam("appKey") String appKey) {
    ResponseEntity<Object> responseEntity = null;

    appKey = StringUtils.trimToEmpty(appKey);
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");

    PassengerQuery passengerQuery = new PassengerQuery();
    passengerQuery.setCompanyId(companyId);
    passengerQuery.setStartTime(start);
    passengerQuery.setEndTime(end);
    passengerQuery.setAppKey(appKey);

    try {

      long num = passengerManager.countByPassengerQuery(passengerQuery);

      Result<Long> result = new Result<>(0, "成功", num);

      responseEntity = new ResponseEntity<>(result, HttpStatus.OK);

      return responseEntity;

    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());

      responseEntity = new ResponseEntity<>(error, HttpStatus.INTERNAL_SERVER_ERROR);

      return responseEntity;
    }
  }


  /**
   * 条件查询新增客户统计明细.
   */
  @RequestMapping(value = "time", method = RequestMethod.GET)
  public ResponseEntity<Object> getPassengers(
          @RequestParam("year") Integer year,
          @RequestParam(required = false, value = "month") Integer month,
          @RequestParam(required = false, value = "companyId") Long companyId,
          @RequestParam(required = false, value = "channel_name") String channelName,
          @RequestParam(required = false, value = "page") Integer page,
          @RequestParam(required = false, value = "rows") Integer rows,
          @RequestParam("appKey") String appKey) {

    ResponseEntity<Object> responseEntity = null;

    appKey = StringUtils.trimToEmpty(appKey);
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");

    try {

      Calendar calendar = Calendar.getInstance();
      calendar.set(Calendar.YEAR, year);
      calendar.set(Calendar.MONTH, month == null ? 0 : month - 1);
      calendar.set(Calendar.DAY_OF_MONTH, 1);
      calendar.set(Calendar.HOUR_OF_DAY, 0);
      calendar.set(Calendar.MINUTE, 0);
      calendar.set(Calendar.SECOND, 0);

      PassengerQuery passengerQuery = new PassengerQuery();
      Long startTime = calendar.getTimeInMillis();
      passengerQuery.setStartTime(startTime);
      
      calendar.set(Calendar.MONTH, month == null ? 11 : month - 1);
      calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
      calendar.set(Calendar.HOUR_OF_DAY, 23);
      calendar.set(Calendar.MINUTE, 59);
      calendar.set(Calendar.SECOND, 59);
      Long endTime = calendar.getTimeInMillis();
      passengerQuery.setEndTime(endTime);
      passengerQuery.setCompanyId(companyId);
      passengerQuery.setChannelName(channelName);
      passengerQuery.setPage(page);
      passengerQuery.setRows(rows);
      passengerQuery.setAppKey(appKey);

      DataGrid<Passenger> dataGrid = passengerManager.findByPassengerQuery(passengerQuery);

      Result<DataGrid<Passenger>> result = new Result<>(0, "成功", dataGrid);

      responseEntity = new ResponseEntity<>(result, HttpStatus.OK);

      return responseEntity;

    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());

      responseEntity = new ResponseEntity<>(error, HttpStatus.INTERNAL_SERVER_ERROR);

      return responseEntity;
    }
  }

  /**
   * 查询来源.
   */
  @RequestMapping(value = "channel", method = RequestMethod.GET)
  public ResponseEntity<Object> getChannel(@RequestParam("appKey") String appKey) {

    ResponseEntity<Object> responseEntity = null;

    appKey = StringUtils.trimToEmpty(appKey);
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");

    try {

      List<String> channels = passengerManager.findChannels(appKey);

      Result<List<String>> result = new Result<>(0, "成功", channels);

      responseEntity = new ResponseEntity<>(result, HttpStatus.OK);

      return responseEntity;

    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());

      responseEntity = new ResponseEntity<>(error, HttpStatus.INTERNAL_SERVER_ERROR);

      return responseEntity;
    }
  }

  /**
   * 根据companyId,乘客名字查询余额总和.
   *
   * @param passengerName (like)
   * @return 返回余额sum
   */
  @RequestMapping(value = {"sumDrawMoney"}, method = RequestMethod.GET)
  public ResponseEntity<Object> sumDrawMoney(
          @RequestParam(required = false, value = "companyId") Long companyId,
          @RequestParam(required = false, value = "passengerName") String passengerName,
          @RequestParam("appKey") String appKey) {
    ResponseEntity<Object> responseEntity = null;
    appKey = StringUtils.trimToEmpty(appKey);
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
    try {

      double sum = passengerManager.sumDrawMoney(companyId, passengerName, appKey);

      Result<Double> result = new Result<>(0, "成功", sum);

      responseEntity = new ResponseEntity<>(result, HttpStatus.OK);

      return responseEntity;

    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());

      responseEntity = new ResponseEntity<>(error, HttpStatus.INTERNAL_SERVER_ERROR);

      return responseEntity;
    }
  }

  /**
   * 修改公司名称 .
   * @param appKey appKey
   * @param companyId 公司id
   * @param abbreviation 公司简称
   */
  @RequestMapping(value = {"updateCompanyName"}, method = RequestMethod.PUT)
  public ResponseEntity<Object> updateCompanyName(
          @RequestParam("appKey") String appKey,
          @RequestParam("companyId") Long companyId,
          @RequestParam("abbreviation") String abbreviation) {
    ResponseEntity<Object> responseEntity = null;
    appKey = StringUtils.trimToEmpty(appKey);
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
    try {

      int passenger = passengerManager.updateCompanyName(companyId,abbreviation,appKey);
      
      enterpriseManager.updateCompanyName(companyId,abbreviation,appKey);
      
      departmentManager.updateCompanyName(companyId,abbreviation,appKey);

      gradeManager.updateCompanyName(companyId,abbreviation,appKey);
      
      Result<Integer> result = new Result<Integer>(0, "成功", passenger);

      responseEntity = new ResponseEntity<>(result, HttpStatus.OK);

      return responseEntity;

    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());

      responseEntity = new ResponseEntity<>(error, HttpStatus.INTERNAL_SERVER_ERROR);

      return responseEntity;
    }
  }


  @RequestMapping(value = "findByIds", method = RequestMethod.GET)
  public ResponseEntity<Object> findByIds(
          @RequestParam(required = false, value = "ids") Long[] ids ,
          @RequestParam(required = false, value = "page") Integer page,
          @RequestParam(required = false, value = "rows") Integer rows,
          @RequestParam("appKey") String appKey) {

    ResponseEntity<Object> responseEntity = null;

    appKey = StringUtils.trimToEmpty(appKey);
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");

    try {

      PassengerQuery passengerQuery = new PassengerQuery();
      passengerQuery.setIds(ids);
      if (page != null) {
        passengerQuery.setPage(page);
      }
      if (rows != null) {
        passengerQuery.setRows(rows);
      }
      passengerQuery.setAppKey(appKey);

      DataGrid<Passenger> dataGrid = passengerManager.findByIds(passengerQuery);
      Result<DataGrid<Passenger>> result = new Result<>(0, "成功", dataGrid);

      responseEntity = new ResponseEntity<>(result, HttpStatus.OK);

      return responseEntity;

    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());

      responseEntity = new ResponseEntity<>(error, HttpStatus.INTERNAL_SERVER_ERROR);

      return responseEntity;
    }
  }
  
  /**
   * 根据推荐类型查询所有客户.
   */
  @RequestMapping(value = "countPassengerByIntroduce", method = RequestMethod.GET)
  public ResponseEntity<Object> countPassengerByIntroduce(
		  @RequestParam("appKey") String appKey,
		  @RequestParam("introduceType") Integer introduceType,
		  @RequestParam(required = false,value="companyId") Integer companyId,
		  @RequestParam(required = false,value="introducer") String introducer) {
    ResponseEntity<Object> responseEntity = null;
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");  
    try {
        Long count = passengerManager.findByIntroduceType(introduceType, introducer,companyId,appKey);
      Result<Long> result = new Result<>(0, "成功", count);
      responseEntity = new ResponseEntity<>(result, HttpStatus.OK);
      return responseEntity;
    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());
      responseEntity = new ResponseEntity<>(error, HttpStatus.INTERNAL_SERVER_ERROR);
      return responseEntity;
    }
  }
  
  /**
   * 统计(去掉集合中的客户)
   */
  @RequestMapping(value = {"findByMultiConditionNew"}, method = RequestMethod.GET)
  public ResponseEntity<Object> findByMultiConditionNew(
		  @RequestParam(required = false, value = "passengerIds") String[] passengerIds,
          @RequestParam(required = false, value = "type") Integer[] type,
          @RequestParam(required = false, value = "gradeId") Long[] grade,
          @RequestParam(required = false, value = "companyId") Long companyId,
          @RequestParam(required = false, value = "companyIds") Long[] companyIds,
          @RequestParam(required = false, value = "permission") Boolean[] permission,
          @RequestParam(required = false, value = "channel_name") String[] channelName,
          @RequestParam(required = false, value = "start") Long start,
          @RequestParam(required = false, value = "end") Long end,
          @RequestParam(required = false, value = "enterpriseId") Long enterpriseId,
          @RequestParam(required = false, value = "departmentId") Long departmentId,
          @RequestParam(required = false, value = "departmentName") String departmentName,
          @RequestParam(required = false, value = "enterpriseName") String enterpriseName,
          @RequestParam(required = false, value = "jobNumber") String jobNumber,
          @RequestParam(required = false, value = "name") String name,
          @RequestParam(required = false, value = "phone") String phone,
          @RequestParam(required = false, value = "carNumber") String carNumber,
          @RequestParam(required = false, value = "page") Integer page,
          @RequestParam(required = false, value = "rows") Integer rows,
          @RequestParam(required = false, value = "serviceType") String serviceType,
          @RequestParam(required = false, value = "serviceTime") Long serviceTime, 
          @RequestParam("appKey") String appKey) {

    ResponseEntity<Object> responseEntity = null;
    appKey = StringUtils.trimToEmpty(appKey);
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空"); 
    
 
    PassengerQuery2 passengerQuery2 = new PassengerQuery2();
    passengerQuery2.setPassengerIds(passengerIds);
    passengerQuery2.setType(type);
    passengerQuery2.setGradeId(grade);
    passengerQuery2.setCompanyId(companyId);
    passengerQuery2.setCompanyIds(companyIds);
    passengerQuery2.setPermission(permission);
    passengerQuery2.setChannelName(channelName);
    passengerQuery2.setStartTime(start);
    passengerQuery2.setEndTime(end);
    passengerQuery2.setEnterpriseId(enterpriseId);
    passengerQuery2.setDepartmentId(departmentId);
    passengerQuery2.setDepartmentName(departmentName);
    passengerQuery2.setEnterpriseName(enterpriseName);
    passengerQuery2.setJobNumber(jobNumber);
    passengerQuery2.setName(name);
    passengerQuery2.setPhone(phone);
    passengerQuery2.setCarNumber(carNumber);
    passengerQuery2.setPage(page);
    passengerQuery2.setRows(rows);
    passengerQuery2.setAppKey(appKey);
    passengerQuery2.setServiceType(serviceType);
    passengerQuery2.setServiceTime(new Date(serviceTime));
    
    try {

      DataGrid<Passenger> dataGrid = passengerManager.findByPassengerQuery3(passengerQuery2);
      
      Result<DataGrid<Passenger>> result = new Result<>(0, "成功", dataGrid);
      
      responseEntity = new ResponseEntity<>(result, HttpStatus.OK) ;

      return responseEntity;

    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());

      responseEntity = new ResponseEntity<>(error, HttpStatus.INTERNAL_SERVER_ERROR);

      return responseEntity;
    }
  }
}
